﻿using DAL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    /// <summary>
    /// 评分表管理类
    /// </summary>
    class DoFeedBack
    {
        DataClasses1DataContext ctx = new DataClasses1DataContext();
        /// <summary>
        /// 获取评分表清单
        /// </summary>
        /// <param name="assessor">评估人</param>
        /// <returns></returns>
        List<T_Feedback> GetFeedback(string assessor)
        {
            List<T_Feedback> result = new List<T_Feedback>();
            try
            {
                result = (List<T_Feedback>)ctx.T_Feedback.Select(p => p.Assessor == assessor && p.ExpirationDate >= DateTime.Now);
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        }
        /// <summary>
        /// 通过受评人获取得分清单
        /// </summary>
        /// <param name="appraisee">受评人</param>
        /// <returns></returns>
        List<T_Feedback> GetFeedbackByAppraisee(string appraisee)
        {
            List<T_Feedback> result = new List<T_Feedback>();
            try
            {
                result = (List<T_Feedback>)ctx.T_Feedback.Select(p => p.Appraisee == appraisee && p.ExpirationDate >= DateTime.Now);
                return result;
            }
            catch (Exception)
            {

                return result;
            }
        }

        void text() {
            List<T_Feedback> result = new List<T_Feedback>();
            var FeedbackListByRelation = (List<T_Feedback>)result.Select(p => p.Relation == "上级"&&p.Status==2);//获取到所有上级的评分清单(已评价的)
            var num = FeedbackListByRelation.Count;//上级人数
            List<Average> aveList = new List<Average>();//平均分集合
            List<Score> oneAllScoreList = new List<Score>();//单项总分集合
            for (int i = 0; i < FeedbackListByRelation.Count; i++)//遍历所有来自上级的评分清单
            {
                Average ave = new Average();//平均分
                ave.Appraisee = FeedbackListByRelation[i].Appraisee;
                ave.Relation = FeedbackListByRelation[i].Relation;
                var oneScoreList = (List<Score>)JsonHepler.GetObj(((T_Feedback)FeedbackListByRelation[i]).Score);//某个人具体评分
                for (int j = 0; j < oneScoreList.Count; j++)
                {
                    // oneAllScoreList[j] 单项总分
                    oneAllScoreList[j].Dimension = oneScoreList[j].Dimension;
                    oneAllScoreList[j].GetScore += oneScoreList[j].GetScore;
                    oneAllScoreList[j].QuestionNO = oneScoreList[j].QuestionNO;
                }
                
                aveList.Add(ave);
            }
            for (int i = 0; i < oneAllScoreList.Count; i++)
            {
                aveList[i].QuestionNO = i + 1;
                aveList[i].GetAverage = oneAllScoreList[i].GetScore / oneAllScoreList.Count;
            }
        }

        void hello(List<Score> scoreList,int questionNO)//按题号获取问卷中该题得分
        {
            var a = scoreList.Single(p => p.QuestionNO == questionNO).GetScore;
        }


    }


}
